package com.learning.spark.scala.sql.implicitly

/**
  * ClassName: ImplicitlyConvert
  * Description: Scala 隐式转换
  * Date: 2018/7/2 15:00 【需求编号】
  *
  * @author Sam Sho
  * @version V1.0.0
  */
object ImplicitlyConvert {

  /**
    * 隐式转换和隐式参数
    *
    * @param obj
    * @return
    */
  implicit def Object2SpecialPerson(obj: Object): SpecialPerson = {
    if (obj.getClass == classOf[Older]) {
      val older = obj.asInstanceOf[Older]
      new SpecialPerson(older.name)
    } else if (obj.getClass == classOf[Child]) {
      val child = obj.asInstanceOf[Child]
      new SpecialPerson(child.name)
    } else {
      println("Nil")
      Nil
    }
  }

  var sumTickets = 0

  def buySpecialTicket(specialPerson: SpecialPerson) = {
    sumTickets += 1
    println(sumTickets)
  }

  def main(args: Array[String]): Unit = {
    val older = new Older("Sam")
    buySpecialTicket(older)

    val child = new Child("Eason")
    buySpecialTicket(child)

    // 循环去做 隐式转换, 程序不会停
    val teacher = new Teacher("Rabby")
    buySpecialTicket(teacher)
  }

}

class SpecialPerson(name: String)

class Older(val name: String)

class Child(val name: String)

class Teacher(val name: String)
